package com.weiso.weisophonebook.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weiso.weisophonebook.HCData;
import com.weiso.weisophonebook.R;
import com.weiso.weisophonebook.bean.LinkMan;
import com.weiso.weisophonebook.bean.LinkManList;

public class LinkManListAdapter2 extends BaseAdapter {
	Context context;
	List<LinkMan> linkmanlist;
	public LinkManListAdapter2(Context context,LinkManList linkmanlist){
		this.context=context;
		this.linkmanlist = linkmanlist.getLinkmanlist();
	}
	public int getCount() {
		// TODO Auto-generated method stub
		return linkmanlist.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return linkmanlist.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		LinkmanListItem linkmanitem;
        if(arg1==null){
        	linkmanitem = new LinkmanListItem();
        	arg1 = LayoutInflater.from(context).inflate(R.layout.linkman_list_item2, null);
        	linkmanitem.name= (TextView) arg1.findViewById(R.id.linkman_list_item2_name);
        	linkmanitem.number= (TextView) arg1.findViewById(R.id.linkman_list_item2_number);
        	linkmanitem.rl2=(RelativeLayout) arg1.findViewById(R.id.linkman_list_item2_rl2);
        	linkmanitem.btn1=(Button) arg1.findViewById(R.id.linkman_list_item2_btn1);
        	linkmanitem.btn2=(Button) arg1.findViewById(R.id.linkman_list_item2_btn2);
        	arg1.setTag(linkmanitem);
        }else{
        	linkmanitem = (LinkmanListItem) arg1.getTag();
        }
        final LinkMan linkman = linkmanlist.get(arg0);
		
       // selection
//        Log.e("adapter", "selection:"+HCData.selection+"-type:"+HCData.type);
        
        if(HCData.type==1){
            SpannableStringBuilder style=new SpannableStringBuilder(linkman.getName());
            
            int start = linkman.getName().indexOf(HCData.selection);
            int end = linkman.getName().indexOf(HCData.selection)+HCData.selection.length();
//            Log.e("adapter", "start:"+start+"-end:"+end);
            
            style.setSpan(new ForegroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            
            linkmanitem.name.setText(style);
            linkmanitem.number.setText(linkman.getNumber());
        }else{
            SpannableStringBuilder style=new SpannableStringBuilder(linkman.getNumber());
            
            int start = linkman.getNumber().indexOf(HCData.selection);
            int end = linkman.getNumber().indexOf(HCData.selection)+HCData.selection.length();
            Log.e("adapter", "start:"+start+"-end:"+end);
            
            style.setSpan(new ForegroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            
            linkmanitem.name.setText(linkman.getName());
            linkmanitem.number.setText(style);
//            if(linkman.isFlg1()){
//            	linkmanitem.ll.setVisibility(View.GONE);
//            }else{
//            	linkmanitem.ll.setVisibility(View.VISIBLE);
//            }
            
        }
//        linkmanitem.rl2.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				
//			}
//		});
        
		return arg1;
	}
    class LinkmanListItem{
    	TextView name;
    	TextView number;
    	RelativeLayout rl2;
    	Button btn1;
    	Button btn2;
	}
}
